﻿using System;
using UnityEngine;

namespace Core
{
    public struct Region : IEquatable<Region>
    {
        public Vector2Int Coord;
        public Vector2Int Size;

        public readonly int Cnt => Size.x * Size.y;

        public readonly Vector2 Center => Coord + (Vector2)Size * 0.5f;

        public void Foreach(Action<Vector2Int> func)
        {
            if (func == null)
            {
                return;
            }
            for (int j = 0; j < Size.y; j++)
            {
                for (int i = 0; i < Size.x; i++)
                {
                    func(Coord.Plus(i, j));
                }
            }
        }

        public static bool operator !=(Region lhs, Region rhs)
        {
            return !(lhs == rhs);
        }

        public static bool operator ==(Region lhs, Region rhs)
        {
            return lhs.Coord == rhs.Coord && lhs.Size == rhs.Size;
        }

        public readonly bool Equals(Region other) => this == other;

        public override readonly bool Equals(object obj)
        {
            if (obj == null || (obj is Region) == false)
            {
                return false;
            }
            return Equals((Region)obj);
        }

        public override readonly int GetHashCode()
        {
            return Coord.GetHashCode() ^ (Size.GetHashCode() << 2);
        }

        public override readonly string ToString()
        {
            return $"({Coord},{Size})";
        }
    }
}
